require('../common/header/index')
require('../common/sidebar/index')
var _util = require('../../util/util')
var _product_service = require('../../service/product-service')
var _header_service = require('../../service/header-service')
var _evaluation_service = require('../../service/evaluation_service');
var _cart_service = require('../../service/cart-service');
const vue = require('../common/vue')
const { baseParse } = require('@vue/compiler-core')
let _product_content = new vue({
    el: '#showItem',
    data() {
        return {
            iteminfo: '',   //商品的属性信息
            itemid: '',
            stock: '',
            qty: 1,
            attribute: [],
            comments: [],
            stars: "",
            baseprice: 0,
            price: 0.0,
            attrdescription: "",
            imgSrc: "", //展示的图片路径
            item: '',   //获取商品的其他信息
            pageid: 1,  //当前页面
            storeItems: [], //当前店铺的商品列表
            pagenum: '',  //获取总页数
            pagesequence: [], //页数列表
        }
    },
    methods: {
        updateList: function (event) {
            _product_detail.getOneItem();
        },
        getAttr: function (event) {
            console.log(this.attribute)
        },
        like: function () {
            let _this = this;
            _evaluation_service.likeProduct(this.itemid, function (res) {
                window.location.href = "/view/product_details.html?itemid=" + _this.itemid;
            }, function (err) {
                console.log(err);
            });
        },
        unlike: function () {
            let _this = this;
            _evaluation_service.unlikeProduct(this.itemid, function (res) {
                window.location.href = "/view/product_details.html?itemid=" + _this.itemid;
            }, function (err) {
                console.log(err);
            });
        },
        changePriceAndImg: function (index, attrIndex) {
            let attrnum = this.iteminfo.length;
            this.price = this.baseprice;
            this.attrdescription = "";
            console.log("num: " + attrnum);
            for (let i = 0; i < attrnum - 1; i++) {
                this.attrdescription = this.attrdescription + " " + $("input:radio[name=" + _product_content.iteminfo[i].attrname + "]:checked").prev().text();
                this.price = this.price * $("input:radio[name=" + _product_content.iteminfo[i].attrname + "]:checked").val();
                console.log("price: " + this.price + " ," + $("input:radio[name=" + _product_content.iteminfo[i].attrname + "]:checked").val());
            }
            this.attrdescription = this.attrdescription + " 单位" + this.iteminfo[attrnum - 1].attrList[0].attrvalue;
            if (index == 0) {
                this.imgSrc = this.iteminfo[0].attrList[attrIndex].src;
            }
        },
        addToCart: function () {
            let _this = this;
            let content = {
                num: _this.qty,
                price: _this.price,
                attrdescription: _this.attrdescription
            }
            console.log(content);
            _cart_service.addToCart(
                _product_content.itemid,
                function (res) {
                    console.log("addTocart success");
                    window.location.href = "/view/product_summary.html";
                },
                function (err) {
                    alert("f");
                    console.log("error in addToCart");
                    window.location.href = "/view/catalog-main.html";
                    console.log(err);
                }, content);
            // alert("after");
        },
        addToCartSImple: function(itemid) {
            _product_service.getOneItem(itemid, function(res) {
                let attrnum = res.length;
                let attrdescription = "";
                let price = res[0].price * res[0].specialrate;
                for (let i = 0; i < attrnum - 1; i++) {
                    attrdescription = attrdescription + " " + res[i].attrList[0].attrvalue;
                    price = price * res[i].attrList[0].rate;
                }
                attrdescription = attrdescription + " 单位" + res[attrnum-1].attrList[0].attrvalue;
                let content = {
                    num: 1,
                    price: price,
                    attrdescription: attrdescription
                }
                _cart_service.addToCart(itemid, function(res) {
                    window.location.href = "/view/product_summary.html";
                }, function(err) {
                    console.log(err);
                }, content);
            }, function(err) {
                console.log(err);
            });
        },
        insertRecord: function () {
            _product_service.insertRecord(this.itemid, function (res) {

            }, function (err) {
                console.log(err);
            })
        },
        //页数处理
        updateList: function (event) {
            _product_details.getShopItems();
        },
        backpage: function (event) {
            if (this.pageid > 1) {
                this.pageid = this.pageid - 1;
                this.updateList();
            }
        },
        toPage: function (pagenum) {
            this.pageid = pagenum;
            this.updateList();
        },
        nextpage: function (event) {
            if (this.pageid * 1 + 1 <= this.total * 1) {
                this.pageid = this.pageid * 1 + 1;
                this.updateList();
            }
        },
        toBlockView: function() {
            $("#listSpan").removeClass("btn-primary");
            $("#blockSpan").addClass("btn-primary");
        },
        toListView: function() {
            $("#blockSpan").removeClass("btn-primary");
            $("#listSpan").addClass("btn-primary");
        }
    },
    created: function () {
        this.itemid = decodeURI(_util.getQueryVariable('itemid'));
        this.insertRecord();
    }
})

let _product_details = {
    _this: this,
    init: function () {
        this.bindEvents();
        this.getOneItem();
        this.fetchComments();
        this.computeAverage();
        this.getItemInfo();
    },
    bindEvents: function () {

    },
    getOneItem: function () {
        _product_service.getOneItem(
            _product_content.itemid,
            function (res) {
                _product_content.iteminfo = res
                _product_content.baseprice = res[0].price;

                for (let i = 0; i < _product_content.iteminfo.length; i++) {
                    for (let j = 0; j < _product_content.iteminfo[i].attrList.length; j++) {
                        var pair = _product_content.iteminfo[i].attrList[j].src.split("\"");
                        _product_content.iteminfo[i].attrList[j].src = "../image/" + pair[1].substring(7);
                    }
                }
                let attrnum = _product_content.iteminfo.length;
                console.log("attrnum: " + attrnum);
                setTimeout(() => {
                    for (let i = 0; i < attrnum; i++) {
                        console.log(" " + i + " :name=" + _product_content.iteminfo[i].attrname + ",value=" + _product_content.iteminfo[i].attrList[0].rate);
                        $("input:radio[name=" + _product_content.iteminfo[i].attrname + "]").eq(0).attr("checked", true);
                    }
                    _product_content.imgSrc = _product_content.iteminfo[0].attrList[0].src
                    _product_content.changePriceAndImg();
                    console.log(_product_content.iteminfo)
                    console.log("success");
                    console.log(res);
                }, 200);
            },
            function (errMsg) {
                console.log(errMsg)
            }
        )
    },
    getShopItems: function () {
        _product_service.getItemsPage(
            _product_content.item.shopid,
            6,
            _product_content.pageid,
            0,
            function (res) {
                _product_content.storeItems = res.records;
                for (let i = 0; i < _product_content.storeItems.length; i++) {
                    var pair = _product_content.storeItems[i].src.split("\"");
                    _product_content.storeItems[i].src = "../image/" + pair[1].substring(7);
                }
                _product_content.pagenum = res.total;
                for (let i = 0; i < res.pages; i++) {
                    _product_content.pagesequence[i] = i * 1 + 1
                }
            },
            function (errMsg) {
                console.log(errMsg)
            }
        )
    },
    fetchComments: function () {
        _evaluation_service.getComments(_product_content.itemid, function (res) {
            _product_content.comments = res;
        }, function (err) {
            console.log(err);
        });
    },
    computeAverage: function () {
        let res = 0.0;
        if (_product_content.comments.length === 0) {
            _product_content.stars = "无";
            return;
        }
        for (let i = 0; i < _product_content.comments.length; i++) {
            res += _product_content.comments[i].star;
        }
        _product_content.stars = res / _product_content.comments.length;
    },
    getItemInfo: function () {
        let self = this;
        _product_service.getOneItemInfo(
            _product_content.itemid,
            function (res) {
                _product_content.item = res;
                self.getShopItems();
            },
            function (errMsg) {
                console.log(errMsg);
            }
        )
    },
}
module.exports = _product_details.init();